
function MainStep2_PickCandi
%
% 绘制近地点庞加莱图，点击鼠标选择轨道初值
%
% 输出：
% picked_LEO2LLO.mat
%
% 作者：张晨
% 邮箱：chenzhang@csu.ac.cn
% 单位：中国科学院空间应用工程与技术中心，空间探索室
% 时间：2022年02月24日
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clc; clear; close all; clear path;
warning off;

% 添加模块
parentFolder = fileparts(pwd);
addpath(genpath([parentFolder , '\Basic']));
addpath(genpath([parentFolder , '\Bcr4bp']));
addpath(genpath([parentFolder , '\Cr3bp']));

global aux candi_LEO2DRO
global h1 h2 h3

% 载入参数
aux = Bcr4bp_Aux;

% 载入上一步结果
load candi_LEO2DRO;

tau0Vec = candi_LEO2DRO(: , 1);
x0Mtx = candi_LEO2DRO(: , 2:5);
taufVec = candi_LEO2DRO(: , 6);
xfMtx = candi_LEO2DRO(: , 7:10);

h1 = figure1;
h2 = figure2;
h3 = figure3;

% 点鼠标画图
figure(3);
scatter(x0Mtx(: , 1) , x0Mtx(: , 2) , 50 , (taufVec - tau0Vec) * aux.dim.EMRot_t / 86400 , 'filled');
hc = colorbar;
hc.Label.String = 'Values';
% clim([70, 120]);

plot_o([0 , 0] , aux.dim.rMagLEO / aux.dim.EMRot_l , 'b--' , 2)

% colormap jet;
xlabel('x/LU');
ylabel('y/LU');
title('Scatter Plot with Colorbar');

dcm_obj = datacursormode(h3);
set(dcm_obj , 'DisplayStyle' , 'datatip' , 'SnapToDataVertex' , 'off' , 'Enable' , 'on')
set(dcm_obj , 'UpdateFcn' , @myupdatefcn);

end



% --------------------------------- 点鼠标画图 ---------------------------------
function output = myupdatefcn(empt , event_obj)

global aux candi_LEO2DRO
global h1 h2 h3

output = [];

tau0Vec = candi_LEO2DRO(: , 1);
x0Mtx = candi_LEO2DRO(: , 2:5);
taufVec = candi_LEO2DRO(: , 6);
xfMtx = candi_LEO2DRO(: , 7:10);

% ----------------------- 提取坐标 --------------------------
clf(h1);
clf(h2);

xTemp = event_obj.Position(1);
yTemp = event_obj.Position(2);

disVec = sqrt((xTemp - x0Mtx(: , 1)).^2 + (yTemp - x0Mtx(: , 2)).^2);
[X , index] = min(disVec);

fprintf('%d \n' , index)

% 反向积分
options = odeset('RelTol' , 1e-12 , 'AbsTol' , 1e-12);
[seg_tt , seg_xx] = ode113(@Bcr4bp_EqmEMRot2D , [taufVec(index) , tau0Vec(index)] , xfMtx(index , :)' , options , aux);

figure(1);
plot(seg_xx(: , 1) , seg_xx(: , 2) , 'b' , 'LineWidth' , 2);
Bcr4bp_ScnEMRot(taufVec(index) , aux);

% DRO初值
aux.periOrb_xi = [0.808936204186703 , 0 , 0 , 0.51563216433164]';
aux.periOrb_P = 3.14159265358979;
options = odeset('RelTol' , aux.tol , 'AbsTol' , aux.tol);
[periOrb_tt , periOrb_xx] = ode113(@Cr3bp_Eqm2D , [0 , aux.periOrb_P] , aux.periOrb_xi , options , aux.EMRot.mu);
aux.periOrb_tt = periOrb_tt;
aux.periOrb_xx = periOrb_xx;
plotOrb2D(periOrb_xx , 'k' , 2)

figure(2);
[tau_SB1Rot , xb2c_SB1Rot] = Bcr4bp_EMRot2SB1RotMtx2D(seg_tt , seg_xx , aux);
Bcr4bp_ScnSB1Rot(tau_SB1Rot(end) , aux);
plot(xb2c_SB1Rot(: , 1) , xb2c_SB1Rot(: , 2) , 'b' , 'LineWidth' , 2);

% 保存
picked_LEO2DRO = candi_LEO2DRO(index , :);
save picked_LEO2DRO picked_LEO2DRO

end
